﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

// Additional Libraries
using Microsoft.Phone.Notification;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Core;
using System.Device.Location;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Net.NetworkInformation;

using LocalDatabase.Model;
using LocalDatabase.ViewModel;
using System.Windows.Controls.Primitives;

namespace FindMe
{
    public partial class chat : PhoneApplicationPage
    {
        ProgressIndicator progress;
        FindMeFriendServiceReference.FindMeFriendServiceClient serviceClient;
        IsolatedStorageSettings settings;
        public chat()
        {
            InitializeComponent();
            anyNews();
            serviceClient = new FindMeFriendServiceReference.FindMeFriendServiceClient();
            serviceClient.SendMessageToFriendCompleted += new EventHandler<FindMeFriendServiceReference.SendMessageToFriendCompletedEventArgs>(serviceClient_SendMessageToFriendCompleted);

            if (App.pushChannel != null)
            {
                App.pushChannel.ShellToastNotificationReceived += new EventHandler<Microsoft.Phone.Notification.NotificationEventArgs>(pushChannel_ShellToastNotificationReceived);
            }

            //<-- Progress bar and clock colors -->
            SystemTray.SetIsVisible(this, true);
            SystemTray.SetOpacity(this, 0.0);
            SystemTray.SetBackgroundColor(this, Colors.Black);
            SystemTray.SetForegroundColor(this, Colors.White);
            progress = new ProgressIndicator();
            progress.IsIndeterminate = true;
            progress.Text = "Loading";
            SystemTray.SetProgressIndicator(this, progress);
            //<-- End progress bar and clock colors -->

            // <-- Settings of isolated storage -->
            settings = IsolatedStorageSettings.ApplicationSettings;

        }
        private void btnHome_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri(App.PathMain, UriKind.Relative));
        }

        private void btnInbox_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri(App.PathConversations, UriKind.Relative));
        }

        private void btnRequests_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri(App.PathNotifications, UriKind.Relative));
        }

        void pushChannel_ShellToastNotificationReceived(object sender, Microsoft.Phone.Notification.NotificationEventArgs e)
        {
            MessageBox.Show("Message Received!");
        }

        void serviceClient_SendMessageToFriendCompleted(object sender, FindMeFriendServiceReference.SendMessageToFriendCompletedEventArgs e)
        {
            MessageBox.Show("Status: " + e.Result);
        }
        
     private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
                string username = settings["Username"].ToString();
                Guid sessionToken = (Guid)settings["SessionToken"];
               // serviceClient.SendMessageToFriendAsync(username, sessionToken, txtToUsername.Text, txtMessage.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void anyNews()
        {

            if (App.counterInbox <= 0)
            {
                borderInbox.Visibility = Visibility.Collapsed;
            }
            else
            {
                borderInbox.Visibility = Visibility.Visible;
                countInbox.Text = App.counterInbox.ToString();
            }
            if (App.counterRequest == 0)
            {
                borderRequest.Visibility = Visibility.Collapsed;
            }
            else
            {
                borderRequest.Visibility = Visibility.Visible;
                countRequest.Text = App.counterRequest.ToString();
            }

            lstChat.Items.Last();
            lstChat.SelectedIndex = (lstChat.Items.Count - 1);
            lstChat.SelectedIndex = -1;
            lstChat.Focus();
        }

        private void Send_Click(object sender, EventArgs e)
        {
            this.lstChat.Items.Add(txtMessage.Text);
            txtMessage.Text = String.Empty;
            lstChat.SelectedIndex = (lstChat.Items.Count - 1);
            lstChat.SelectedIndex = -1;
            lstChat.Focus();
        }

        
        private void Clear_Click(object sender, EventArgs e)
        {
            txtMessage.Text = String.Empty;
            lstChat.Focus();
            
        }

        private void txtMessage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.lstChat.Items.Add(txtMessage.Text);
                txtMessage.Text = String.Empty;
                lstChat.SelectedIndex = (lstChat.Items.Count - 1);
                lstChat.SelectedIndex = -1;
                lstChat.Focus();
            }
        }

        private void txtMessage_GotFocus(object sender, RoutedEventArgs e)
        {
            txtMessage.Background = new SolidColorBrush(Colors.Transparent);          
            txtMessage.Foreground = new SolidColorBrush(Colors.White);
                    
        }

        private void txtMessage_LostFocus(object sender, RoutedEventArgs e)
        {
         txtMessage.Foreground = new SolidColorBrush(Colors.White);      
        }

        private void appRefresh_Click(object sender, EventArgs e)
        {
            progress.IsVisible = true;
            anyNews();
            progress.IsVisible = false;  
        }

        private void appSettings_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri(App.PathSettings, UriKind.Relative));
        }

        private void appLogOut_Click(object sender, EventArgs e)
        {
            progress.IsVisible = true;
            string username = settings["Username"].ToString();
            Guid sessionToken = (Guid)settings["SessionToken"];

            serviceClient.LogOffAsync(username, sessionToken);
            settings["SessionToken"] = new Guid();
            settings.Save();
            progress.IsVisible = false;
            NavigationService.Navigate(new Uri(App.PathLogin, UriKind.Relative));
        }

    }
}